package wnhz.com.business.groupshop.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.groupshop.bean.F3DongTaiXiangQBean;
import wnhz.com.business.groupshop.bean.PingLunShowBean;
import wnhz.com.business.view.CircularImage;
import wnhz.com.business.view.NoScrollGridView;

public class DetailsActivity extends BaseActivity implements View.OnClickListener {

    private ImageView tv_finish;
    private RelativeLayout rl_close;
    private RecyclerView comment_recycler;
    private NoScrollGridView gridview;
    private ArrayList<String> imagePaths = new ArrayList<>();
    private String tid;
    private TextView et_pinglun;
    private String pinglun;
    private CircularImage iv_dt_head;
    private TextView tv_dt_name;
    private TextView tv_dt_time;
    private TextView tv_message;
    private TextView tv_label;
    private RelativeLayout rl_viedeo;
    private ImageView iv_collect_image;
    private List<F3DongTaiXiangQBean.InfoBean.UserTalkBean> userTalkBeen = new ArrayList<>();
    private List<PingLunShowBean.InfoBean.DisBean> disBeen = new ArrayList<>();
    private LinearLayout ll_zan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        initView();
    }

    private void initView() {
//        tv_message = (TextView) findViewById(R.id.tv_message);
        gridview = (NoScrollGridView) findViewById(R.id.gridview);
        et_pinglun = (TextView) findViewById(R.id.et_pinglun);
        et_pinglun.setOnClickListener(this);
        comment_recycler = (RecyclerView) findViewById(R.id.comment_recycler);
        comment_recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    private List<String> list = new ArrayList<>();
    private DetailAdapter detailAdapter;

    private void initData() {
        detailAdapter = new DetailAdapter(this, disBeen);
        comment_recycler.setAdapter(detailAdapter);
    }

    private ImageView iv_praise;

    class DetailAdapter extends BaseRVAdapter {
        /**
         * 构造器
         *
         * @param context 上下文
         * @param list    数据集合
         */
        public DetailAdapter(Context context, @NonNull List list) {
            super(context, list);
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.item_comment_recycler;
        }

        @Override
        public void onBind(BaseViewHolder holder, final int position) {
            ImageView iv_head = holder.getView(R.id.iv_head);

            Glide.with(DetailsActivity.this).load("http://img4.imgtn.bdimg.com/it/u=2644422079,4250545639&fm=21&gp=0.jpg").into(iv_head);
//            RecyclerView recyclerView = holder.getView(R.id.comment_item_recycler);
//            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
//            recyclerView.setAdapter(new BaseRVAdapter(mContext, disBeen.get(position).getReply()) {
//                @Override
//                public int getLayoutId(int viewType) {
//                    return R.layout.item2_comment_recycler;
//                }
//
//                @Override
//                public void onBind(final BaseViewHolder holder, final int position2) {
//                    final TextView text = holder.getTextView(R.id.comment_item_item);
//                    text.setText(Html.fromHtml("<b><font color=\"#333333\">" + disBeen.get(position).getReply().get(position2).getUsername()
//                            + "  回复  " + disBeen.get(position).getReply().get(position2).getName() + "：</font></b><font color=\"#666666\">" + disBeen.get(position).getReply().get(position2).getContent() + "</font>"));
//                    text.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            String token = disBeen.get(position).getReply().get(position2).getToken();
//                            if (token.equals(MyApplication.getInstance().userBean.getToken())) {
//                                tv_remove.setText("删除");
//                            } else {
//                                tv_remove.setText("举报");
//                            }
//                            bottomView.setCancelable(true);
//                            bottomView.show();
//                            //删除
//                            bottomView.findViewById(R.id.tv_remove).setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    if (tv_remove.getText().toString().contains("删除")) {
//                                        bottomView.dismiss();
//                                        Map<String, Object> map = new HashMap<>();
//                                        if (MyApplication.getInstance().userBean != null) {
//                                            map.put("token", MyApplication.getInstance().userBean.getToken());
//                                        }
//                                        map.put("type", 1);//评论类型
//                                        map.put("id", disBeen.get(position).getReply().get(position2).getDiscuss_id());
//                                        upPLReMove(map);
//                                    } else {
//                                        Map<String, Object> map = new HashMap<>();
//                                        if (MyApplication.getInstance().userBean != null) {
//                                            map.put("token", MyApplication.getInstance().userBean.getToken());
//                                        }
////                                        map.put("type",1);//评论类型
//                                        map.put("id", disBeen.get(position).getReply().get(position2).getDiscuss_id());
//                                        upPLJBap(map);
//                                    }
//                                }
//                            });
//                            //取消
//                            bottomView.findViewById(R.id.tv_close).setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    bottomView.dismiss();
//                                }
//                            });
//                            bottomView.findViewById(R.id.tv_huifu).setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    bottomView.dismiss();
//
//                                    text.setMaxLines(Integer.MAX_VALUE);
//                                    String hint = "回复 " + disBeen.get(position).getUsername();
//                                    showInputComment(DetailsActivity.this, hint, new CommentDialogListener() {
//                                        @Override
//                                        public void onClickPublish(final Dialog dialog, EditText input, final TextView btn) {
//                                            final String content = input.getText().toString();
//                                            if (content.trim().equals("")) {
//                                                Toast.makeText(DetailsActivity.this, "评论不能为空", Toast.LENGTH_SHORT).show();
//                                                return;
//                                            }
//                                            btn.setClickable(false);
//                                            //回复
//                                            Map<String, Object> map = new HashMap<>();
//                                            if (MyApplication.getInstance().userBean.getToken() != null) {
//                                                map.put("token", MyApplication.getInstance().userBean.getToken());
//                                            }
//                                            map.put("did", disBeen.get(position).getReply().get(position2).getDiscuss_id());//评论ID 初始为0
//                                            map.put("dis_tye", 1);//评论类型
//                                            map.put("type_id", tid);//评论动态 id
//                                            map.put("group", disBeen.get(position).getReply().get(position2).getGroup());//评论类型
//                                            map.put("content", content);//评论内容
//                                            upPingLun(map);
//                                            dialog.dismiss();
//                                        }
//
//                                        @Override
//                                        public void onShow(int[] inputViewCoordinatesInScreen) {
//                                        }
//
//                                        @Override
//                                        public void onDismiss() {
//                                        }
//                                    });
//                                }
//                            });
//
//                        }
//                    });
//                }
//
//            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //评论弹窗
            case R.id.et_pinglun:
                String hint = "我也说一句...";
                showInputComment(DetailsActivity.this, hint, new CommentDialogListener() {
                    @Override
                    public void onClickPublish(final Dialog dialog, EditText input, final TextView btn) {
                        final String content = input.getText().toString();
                        if (content.toString().equals("")) {
                            Toast.makeText(DetailsActivity.this, "评论不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
//                        //评论
//                        Map<String, Object> map = new HashMap<>();
//                        if (MyApplication.getInstance().userBean.getToken() != null) {
//                            map.put("token", MyApplication.getInstance().userBean.getToken());
//                        }
//                        map.put("did", 0);//评论ID 初始为0
//                        map.put("dis_tye", 1);//评论类型
//                        map.put("type_id", tid);//评论动态 id
//                        map.put("group", 0);//评论类型
//                        map.put("content", content);//评论内容
//                        upPingLun(map);
                        dialog.dismiss();
                        Toast.makeText(DetailsActivity.this, "评论成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onShow(int[] inputViewCoordinatesInScreen) {

                    }

                    @Override
                    public void onDismiss() {

                    }
                });
                break;
        }
    }

    /**
     * 弹出评论对话框
     */
    private static Dialog showInputComment(Activity activity, CharSequence hint, final CommentDialogListener listener) {
        final Dialog dialog = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar);
        dialog.setContentView(R.layout.view_input_comment);
        dialog.findViewById(R.id.input_comment_dialog_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (listener != null) {
                    listener.onDismiss();
                }
            }
        });
        final EditText input = (EditText) dialog.findViewById(R.id.input_comment);
        input.setHint(hint);
        final TextView btn = (TextView) dialog.findViewById(R.id.btn_publish_comment);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClickPublish(dialog, input, btn);
                }
            }
        });
        dialog.setCancelable(true);
        dialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (listener != null) {
                    int[] coord = new int[2];
                    dialog.findViewById(R.id.input_comment_container).getLocationOnScreen(coord);
                    // 传入 输入框距离屏幕顶部（不包括状态栏）的长度
                    listener.onShow(coord);
                }
            }
        }, 300);
        return dialog;
    }

    public interface CommentDialogListener {
        void onClickPublish(Dialog dialog, EditText input, TextView btn);

        void onShow(int[] inputViewCoordinatesOnScreen);

        void onDismiss();
    }


}
